package com.yh.controller;

import com.yh.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.yh.model.Admin;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

/**
 * Created by YH on 2017/8/28.
 */
//@Controller
//@RequestMapping
@RestController
public class AdminController {

        @Autowired
        private AdminService adminService;


        /**
         * 用户信息查询 api
         * @param userName
         * @return
         */
        @RequestMapping(value = "/api/admin/getUser",method = RequestMethod.GET)
        public Admin findOneUser(@RequestParam(value = "userName", required = true) String userName){
            return adminService.findByName(userName);
        }

        /**
         * 用户登录 api
         * @param userName
         * @param password
         * @return
         */
        @RequestMapping(value = "/api/admin/login",method = RequestMethod.POST)
        public Admin findByNamePassword(@RequestParam(value = "userName",required = true) String userName,
                                        @RequestParam(value = "password") String password){
            return adminService.findByNameAndPassword(userName,password);
        }

        @RequestMapping(value = "api/admin/register",method = RequestMethod.POST)
        public Admin addAdminUser(@RequestParam(value = "userName",required = true) String userName,
                                  @RequestParam(value = "password") String password){
              adminService.addAdminUser(userName, password);
                if (adminService.findByNameAndPassword(userName, password).equals("")==false){
                       return adminService.findByNameAndPassword(userName, password);
                }
                return adminService.findByNameAndPassword(userName,password);
        }

    //    @Autowired
//    private AdminService adminService;
//
//    /**
//     * 登录跳转
//     *
//     * @param model
//     * @return
//     */
//    @GetMapping("/admin/login")
//    public String loginGet(Model model) {
//        return "login";
//    }
//
//    /**
//     * 登录
//     *
//     * @param admin
//     * @param model
//     * @param httpSession
//     * @return
//     */
//    @PostMapping("/admin/login")
//    public String loginPost(Admin admin, Model model, HttpSession httpSession) {
//        Admin adminRes = adminService.findByNameAndPassword(admin);
//        if (adminRes != null) {
//            httpSession.setAttribute("admin", adminRes);
//            return "redirect:dashboard";
//        } else {
//            model.addAttribute("error", "用户名或密码错误，请重新登录！");
//            return "login";
//        }
//    }
//
//    /**
//     * 注册
//     *
//     * @param model
//     * @return
//     */
//    @GetMapping("/admin/register")
//    public String register(Model model) {
//        return "register";
//    }

}
